package com.cml.dm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cml.dm.admin.entity.DmMetadata;
import com.cml.dm.admin.mapper.DmExeLoggerMapper;
import com.cml.dm.admin.mapper.DmMetadataMapper;
import com.cml.dm.admin.service.DmContextInitService;
import com.cml.dm.engine.config.DmEngineXmlParser;
import com.cml.dm.engine.context.DmContext;
import com.cml.dm.engine.model.Node;
import com.cml.dm.engine.model.Task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DmContextMetaInitService  implements DmContextInitService {
    @Resource
    private DmMetadataMapper dmMetadataMapper;

    public void init() {
       log.info("元数据初始化开始，读取文件:"+DmContext.DM_META_FILE_NAME);
       List<Task> taskList= new ArrayList<Task>();
       DmEngineXmlParser.parser(taskList,DmContext.DM_META_FILE_NAME);
       DmContext.CUR_TASK_LIST=taskList;
       for (Task task : taskList) {
           if(!checkTaskVersion(task)){
               log.info("任务{}开始升级元数据！",task.getId());
               List<Node> nodeList = task.getNodeList();
               String first = null;
               for (Node node : nodeList) {
                   if(first == null){
                       first = node.getId();
                   }
                   DmMetadata dmMetadata = new DmMetadata();
                   dmMetadata.setTaskID(task.getId());
                   dmMetadata.setVersion(Double.parseDouble(task.getVersion()));
                   dmMetadata.setNodeId(node.getId());
                   dmMetadata.setTable(node.getTable());
                   dmMetadata.setPk(node.getPk());
                   dmMetadata.setJoinUpPkNode(node.getJoinUpPkNode());
                   dmMetadata.setNext(node.getNext());
                   dmMetadata.setBackupSql(node.getBackupSql());
                   dmMetadata.setRecoverySql(node.getRecoverySql());
                   dmMetadata.setFirst(first);
                   dmMetadata.setJoinNodeId(node.getJoinNodeId());
                   dmMetadata.setShardingColumn(node.getShardingColumn());

                   dmMetadata.setOrgId(node.getOrgId());

                   //1是懒加载
                   dmMetadata.setLazy(task.getLazy()?1:0);
                   dmMetadata.setThreadNum(task.getThreadNum());
                   dmMetadata.setLimit(node.getLimit());
                   dmMetadata.setSortField(node.getSortField());
                   dmMetadataMapper.insert(dmMetadata);
               }
               log.info("任务{}元数据升级结束！",task.getId());
           }else{
               log.info("任务{}版本为最新版本！",task.getId());
           }
       }
       log.info("元数据初始化结束");
    }

    /**
     * 检查当前版本是否存在
     * @param task
     * @return
     */
    private boolean checkTaskVersion(Task task){
        QueryWrapper<DmMetadata> queryWrapper = new QueryWrapper<DmMetadata>();
        queryWrapper.lambda().eq(DmMetadata::getTaskID, task.getId());
        queryWrapper.lambda().eq(DmMetadata::getVersion, task.getVersion());
        List dmMetadataList = dmMetadataMapper.selectList(queryWrapper);
        return dmMetadataList != null&& dmMetadataList.size()>0 ? true : false;
    }
}
